<template>
	<view style="margin: 10rpx;border-radius: 10rpx;">
		<u--form
							labelPosition="left"
							:model="meeting"
							:rules="rules"
							ref="form1"
					>
						<u-form-item
								label="会议室名称"
								prop="userInfo.userId"
								borderBottom
								ref="item1"
						>
							<u--input
									v-model="meeting.meetingName"
									border="none"
							></u--input>
						</u-form-item>
						<u-form-item
								label="房间号"
								prop="userInfo.userId"
								borderBottom
								ref="item1"
						>
							<u--input
									v-model="meeting.room"
									border="none"
							></u--input>
						</u-form-item>
						<u-form-item
								label="楼栋号"
								prop="userInfo.userId"
								borderBottom
								ref="item1"
						>
							<u--input
									v-model="meeting.building"
									border="none"
							></u--input>
						</u-form-item>
						<u-form-item
								label="人数"
								prop="userInfo.userId"
								borderBottom
								ref="item1"
						>
							<u--input
									v-model="meeting.people"
									border="none"
							></u--input>
						</u-form-item>
						<u-form-item
								label="描述"
								prop="userInfo.password"
								borderBottom
								ref="item1"
						>
							<u--input
									v-model="meeting.description"
									border="none"
							></u--input>
						</u-form-item>
						
					
					</u--form>
				</u--form>
				<u-button @click="insertMeeting" v-if="option_button===1" style="border-radius: 5%;width: 350rpx;background-color: #53C21D;" >添加</u-button>
				<view v-else style="display: inline-flex;margin: 100rpx 90rpx;">
					<u-button @click="updateMeeting" style="border-radius: 5%;width: 250rpx;background-color: #53C21D;margin-right: 50rpx;">更新</u-button>
					<u-button @click="deleteMeeting" style="background-color: #ff0000;border-radius: 5%;width: 250rpx;" >删除</u-button>
				</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				meeting:{
					id:'',
					meetingName:'',
					room:'',
					building:'',
					description:'',
					people:'',
				},
				option_button:1
			}
		},
		onLoad(options) {
			if(JSON.parse(decodeURIComponent(options.item))!=null){
				let data = JSON.parse(decodeURIComponent(options.item))
				this.meeting.meetingName = data.meetingName
				this.meeting.room = data.room
				this.meeting.building = data.building
				this.meeting.description = data.description
				this.meeting.people = data.people
				this.meeting.id = data.id
				this.option_button = 2
			}
			console.log(JSON.parse(decodeURIComponent(options.item)))
		},
		methods: {
			deleteMeeting(){
				uni.request({
					url:'/api/meeting/deleteMeetingById?meetingId='+this.meeting.id,
					method:'get',
					success(res) {
						if(res.data.code===200){
							uni.showToast({
								icon: 'success',
								position: 'bottom',
								title: '删除成功!'
							})
							uni.navigateBack({
								delta:1
							})
						}else{
							uni.showToast({
								icon: 'error',
								position: 'bottom',
								title: '出错'
							})
							}
				
					}})
			},
			updateMeeting(){
				uni.request({
					url:'/api/meeting/updateMeeting',
					method:'post',
					 header:{
					            'content-type':'application/x-www-form-urlencoded'
					        },
					data:this.meeting,
					success(res) {
						if(res.data.code===200){
							uni.showToast({
								icon: 'success',
								position: 'bottom',
								title: '更新成功!'
							})
							uni.navigateBack({
								delta:1
							})
						}else{
							uni.showToast({
								icon: 'error',
								position: 'bottom',
								title: '出错'
							})
							}
				
					}})
			},
			insertMeeting(){
				if(this.meeting.meetingName===''||
				this.meeting.room===''||
				this.meeting.building===''||
				this.meeting.people===''){
					uni.showToast({
						icon: 'error',
						position: 'bottom',
						title: '请输入信息'
					})
				}else{
					uni.request({
						url:'/api/meeting/addMeeting',
						method:'post',
						 header:{
						            'content-type':'application/x-www-form-urlencoded'
						        },
						data:this.meeting,
						success(res) {
							if(res.data.code===200&&res.data.data!=0){
								uni.showToast({
									icon: 'success',
									position: 'bottom',
									title: '添加成功!'
								})
								uni.navigateBack({
									delta:1
								})
							}else{
								uni.showToast({
									icon: 'error',
									position: 'bottom',
									title: '会议室已存在!'
								})
								}
					
						},
						fail() {
							uni.showToast({
								icon: 'error',
								position: 'bottom',
								title: '出错'
							})
						}
				})
				}
			}
		}
	}
</script>

<style>

</style>
